import { Matrix4, Vector3 } from "three";

export default class GlobalMath {
    public static computePositionForMatrix(matrix: Matrix4, unChangePoint: Vector3, resultPoint: Vector3){
        let firstRow = new Vector3(matrix.elements[0], matrix.elements[4], matrix.elements[8])
        let camraPositionX = resultPoint.x - firstRow.dot(unChangePoint);

        let secondRow = new Vector3(matrix.elements[1], matrix.elements[5], matrix.elements[9])
        let camraPositionY = resultPoint.y - secondRow.dot(unChangePoint);

        let thirdRow = new Vector3(matrix.elements[2], matrix.elements[6], matrix.elements[10])
        let camraPositionZ = resultPoint.z - thirdRow.dot(unChangePoint);

        let fourthRow = new Vector3(matrix.elements[3], matrix.elements[7], matrix.elements[11])
        let camraPositionW = 1 - fourthRow.dot(unChangePoint);

        return new Vector3(camraPositionX / camraPositionW, camraPositionY / camraPositionW, camraPositionZ / camraPositionW);
    }
}